package vip.specimen.system.service;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import vip.specimen.system.datasource.entity.SystemRoleEntity;
import vip.specimen.system.repository.SystemRoleRepository;
import vip.specimen.system.datasource.validation.SystemRoleValidation;
import vip.specimen.system.utils.CommonUtil;
import vip.specimen.system.utils.DataBaseUtil;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Calendar;
import java.util.List;
import java.util.Set;
import java.util.UUID;


/**
 * *@Author: 余浪人
 * *@Date：2021/2/22 16:03
 * *@Email:yulangren520@Gmail.com
 * *@QQ:1368419640
 **/
@Service
@Slf4j
public class SystemRoleService {
    @Resource
    private SystemRoleRepository systemRoleRepository;
    @Resource
    private SystemPermissionService systemPermissionService;
    @Resource
    private HttpServletRequest request;

    /**
     * 新增 角色
     *
     * @param roleValidation 角色验证对象
     * @return 当前角色对象
     */
    @Transactional
    public SystemRoleEntity insert(SystemRoleValidation roleValidation) {
        SystemRoleEntity roleEntity = new SystemRoleEntity(roleValidation.getRoleName(), roleValidation.getRoleCode(), roleValidation.getRoleDescription(), roleValidation.getRoleStatus(), roleValidation.getRoleRemark(), this.systemPermissionService.findAllById(roleValidation.getPermission()));
        return this.systemRoleRepository.save(roleEntity);
    }

    /**
     * 根据角色标识查询
     *
     * @param s 角色标识
     * @return 角色
     */
    public SystemRoleEntity findSystemRoleEntityByRoleCode(String s) {
        return this.systemRoleRepository.findSystemRoleEntityByRoleCode(s);
    }

    /**
     * 分页查询全部角色
     *
     * @return result
     */
    public Page<SystemRoleEntity> findAll() {
        JSONObject param = CommonUtil.pageParams(this.request);
        return this.systemRoleRepository.findAll(DataBaseUtil.getPageable(param.getIntValue("page"), param.getIntValue("pageSize"), DataBaseUtil.getSort("desc", "createTime")));
    }

    /**
     * 获取全部角色
     *
     * @return result
     */
    public List<SystemRoleEntity> findRoleAll() {
        return this.systemRoleRepository.findAll();
    }

    /**
     * 根据uuid 更新 角色信息
     *
     * @param uuid           uuid
     * @param roleValidation 验证体
     * @return 当前角色
     */
    @Transactional
    public SystemRoleEntity update(UUID uuid, SystemRoleValidation roleValidation) {
        try {
            if (this.systemRoleRepository.existsById(uuid)) {
                SystemRoleEntity roleEntity = this.systemRoleRepository.findById(uuid).get();
                roleEntity.setRoleCode(roleValidation.getRoleCode());
                roleEntity.setPermission(this.systemPermissionService.findAllById(roleValidation.getPermission()));
                roleEntity.setRoleDescription(roleValidation.getRoleDescription());
                roleEntity.setRoleName(roleValidation.getRoleName());
                roleEntity.setRoleStatus(roleValidation.getRoleStatus());
                roleEntity.setRoleRemark(roleValidation.getRoleRemark());
                roleEntity.setUpdateTime(Calendar.getInstance().getTime());
                return this.systemRoleRepository.save(roleEntity);
            }
        } catch (Exception e) {
            log.error("更新角色异常:" + e.getMessage());
        }
        return null;
    }

    /**
     * 根据uuid删除
     *
     * @param uuid uuid
     * @return res
     */
    @Transactional
    public boolean delete(UUID uuid) {
        if (this.systemRoleRepository.existsById(uuid)) {
            this.systemRoleRepository.deleteById(uuid);
            return true;
        }
        return false;
    }

    /**
     * 根据uuid集查询
     *
     * @param uuids uuid
     * @return result
     */
    public List<SystemRoleEntity> findAllById(Set<UUID> uuids) {
        return this.systemRoleRepository.findAllById(uuids);
    }

}
